package com.syedu.day13.homework1;

import java.util.ArrayList;
import java.util.List;

/**
 * description：
 * @author Administrator
 * @date 2022/3/28 9:31
 */
public class StudentServiceImpl implements StudentService {
    List<Student> students = new ArrayList<>();

    @Override
    public boolean insertStudent(Student student) {
        return students.add(student);
    }

    @Override
    public boolean deleteStudent(Long id) {
        return students.removeIf(student -> student.getId().equals(id));
    }

    @Override
    public void updateStudent(Long id, String name, Integer age) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                if (name != null) {
                    students.get(i).setName(name);
                }
                if (age != null) {
                    students.get(i).setAge(age);
                }
                break;
            }
        }
    }

    @Override
    public void selectAllStudents() {
        students.forEach(System.out::println);
    }
}